package com.xw.controller;

import com.xw.common.Constants;
import com.xw.common.Result;
import com.xw.entity.Subject;
import com.xw.req.PageRequest;
import com.xw.service.SubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xw
 * &#064;Date  2024/12/5 20:18
 * 课程的控制器
 */
@RestController
@RequestMapping("/subject")
@Api(tags = "课程管理")
public class SubjectController {
    
    
    @Resource
    private SubjectService subjectService;


    @PostMapping("/create")
    @ApiOperation("新增课程")
    public Result<?> publishSubject(@RequestBody @Validated Subject subject) {
        subjectService.insertSubject(subject);
        return new Result<>(Constants.SUCCESS);
    }

    @PostMapping("/update")
    @ApiOperation("修改课程")
    public Result<?> updateSubject(@RequestBody @Validated Subject subject) {
        subjectService.updateSubject(subject);
        return new Result<>(Constants.SUCCESS);
    }


    /**
     * 删除课程
     * */
    @DeleteMapping("/{id}")
    @ApiOperation("删除课程")
    public Result<?> deleteSubject(@PathVariable("id") Long id) {
        subjectService.deleteSubject(id);
        return new Result<>(Constants.SUCCESS);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID获取课程详情")
    public Result<?> getSubjectById(@PathVariable("id") Long id) {
        Subject subject = subjectService.getById(id);
        if (subject != null) {
            return new Result<>(subject, Constants.SUCCESS);
        } else {
            return new Result<>(Constants.ERROR, "课程不存在");
        }
    }
    

    @GetMapping("/all")
    @ApiOperation("获取所有课程列表")
    public Result<?> getAllSubjects() {
        List<Subject> subject = subjectService.getAllSubjects();
        return new Result<>(subject, Constants.SUCCESS);
    }

    @PostMapping("deleteBatchByIds")
    @ApiOperation("通过ID批量删除课程信息")
    public Result<?> deleteBatchByIds(@RequestBody List<Integer> ids) {
        subjectService.deleteBatchByIds(ids);
        return new Result<>(Constants.SUCCESS);
    }


    @PostMapping("page")
    @ApiOperation("分页获取课程列表")
    public Result<?> page(@RequestBody PageRequest<Subject> pageRequest) {
        return new Result<>(subjectService.page(pageRequest), Constants.SUCCESS);
    }


    // 根据分类id 获取课程列表
    @GetMapping("/listByCategoryId/{id}")
    @ApiOperation("根据分类id获取课程列表")
    public Result<?> getSubjectListByCategoryId(@PathVariable("id") Long id) {
        List<Subject> subjectList = subjectService.lambdaQuery()
                .eq(Subject::getCategoryId, id)
                .list();
        return new Result<>(subjectList, Constants.SUCCESS);
    }
    
    
}
